package hot.job.autoconfigure.runner;

import hot.job.core.holder.HotJobMetaHolder;
import hot.job.core.meta.JobMeta;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.util.StringUtils;
import java.util.Map;

/**
 * application runner to bootstrap hot jobs
 *
 * @author gaowenhao
 * @since 2022/7/11 21:14
 */
public class HotJobRunner implements ApplicationRunner {

    @Autowired
    private Scheduler scheduler;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        Map<String, JobMeta> metaMap = HotJobMetaHolder.getReadableMap();
        metaMap.values().forEach(this::bootstrapJob);
    }

    private void bootstrapJob(JobMeta meta){
        //TODO: bootstrap job with clustered mode or non-clustered mode
        if(meta.getHot()){
            JobDetail job = JobBuilder.newJob(meta.getJobClass()).withIdentity(meta.getName(), meta.getGroup()).build();
            Trigger trigger = TriggerBuilder.newTrigger().withIdentity(meta.getName(), meta.getGroup()).withSchedule(CronScheduleBuilder.cronSchedule(StringUtils.hasText(meta.getCron()) ? meta.getCron() : "")).build();

        }else{

        }
    }

}
